#include "Check.h"
#include "CastFile.h"
#include "Init.h"

int main(int argc, char *argv[])
{

    std::string str1 = argv[1];
    std::string str2 = argv[2];
    std::string str3 = argv[3];

    // 用于初始化生成对比库库
    if (str1 == "init")
    {
        std::filesystem::path file_dir = str2;
        std::filesystem::path cast_file_dir = str3;
        init(file_dir, cast_file_dir);
        return 0;
    }

    CastFile student(str1, str2);
    std::filesystem::path cast_lib_dir = str3;

    process_cast_file(student.records, student.cast_file, student.copy_str);
    records_join(student.records, student.records_string);

    student.similarity = check_similarity(student.records_string, cast_lib_dir);

    write_to_cast_lib(student.records_string, student.cast_file, cast_lib_dir);

    int max_print = 10;
    int total = student.similarity.size();
    int count = 0;

    std::cout << "Showing " << std::min(max_print, total) << " of " << total << " results:\n";

    for (const auto &[path, sim] : student.similarity)
    {
        if (count++ >= max_print)
            break;

        std::cout << "[" << std::setw(6) << std::setprecision(4) << sim << "%] "
                  << std::filesystem::relative(path,cast_lib_dir)
                  << std::endl;
    }

    std::cout << "The following is suspected to be a copy: " << std::endl;
    for (auto it = student.copy_str.begin(); it != student.copy_str.end(); ++it)
    {
        std::cout << *it << std::endl;
    }

    return 0;
}
